package battlestein;
/**
 * 
 * @author LocoRocoBattleStein
 *
 */
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import base.Base;
import base.BaseManager;

	public class Agent {
		private int idAgent;
		private int idPlayer;
		private Image[] imgAgent;

		private double x;
		private double y;
		private double dirX;
		private double dirY;
		
		private Base baseSrc;
		private Base baseDest;
		private Base[] path;
		private int index_path;
		
		private double velocity;
		private int life;
		private int cost;
		private int diameter;
		/**
		 * Constructor
		 * @param idPlayer
		 * @param baseSrc
		 * @param dirX
		 * @param dirY
		 * @param baseDest
		 * @param velocity
		 * @param life
		 * @param cost
		 */
		public Agent(int idPlayer, Base baseSrc, double dirX, double dirY, Base baseDest, double velocity, int life, int cost) {
			this.diameter = 15;
			this.idPlayer = idPlayer;
			this.baseSrc = baseSrc;
			this.x = baseSrc.getX();
			this.y = baseSrc.getY();
			this.dirX = dirX;
			this.dirY = dirY;
			this.baseDest = baseDest;
			this.velocity = velocity;
			this.life = life;
			this.cost = cost;
			
			try {
				this.imgAgent = new Image[5];
				imgAgent[0] = ImageIO.read(new File("medias/locoRocoBleu.png"));
				imgAgent[1] = ImageIO.read(new File("medias/locoRocoBleu.png"));
				imgAgent[2] = ImageIO.read(new File("medias/locoRocoJaune.png"));
				imgAgent[3] = ImageIO.read(new File("medias/locoRocoRose.png"));
				imgAgent[4] = ImageIO.read(new File("medias/locoRocoViolet.png"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			};
		}
		
		public int getIdAgent() {
			return idAgent;
		}
		public void setIdAgent(int idAgent) {
			this.idAgent = idAgent;
		}

		public int getIdPlayer() {
			return idPlayer;
		}
		public void setIdPlayer(int idPlayer) {
			this.idPlayer = idPlayer;
		}
		
		public double getX() {
			return x;
		}
		public void setX(double x) {
			this.x = x;
		}

		public double getY() {
			return y;
		}
		public void setY(double y) {
			this.y = y;
		}
		
		public double getDirX() {
			return dirX;
		}
		public void setDirX(double dirX) {
			this.dirX = dirX;
		}
		public double getDirY() {
			return dirY;
		}
		public void setDirY(double dirY) {
			this.dirY = dirY;
		}
		
		public Base getBaseSrc() {
			return baseSrc;
		}
		public void setBaseSrc(Base baseSrc) {
			this.baseSrc = baseSrc;
		}

		public Base getBaseDest() {
			return baseDest;
		}

		public void setBaseDest(Base baseDest) {
			this.baseDest = baseDest;
		}

		public double getVelocity() {
			return velocity;
		}

		public void setVelocity(double velocity) {
			this.velocity = velocity;
		}

		public int getLife() {
			return life;
		}
		public void setLife(int life) {
			this.life = life;
		}

		public int getCost() {
			return cost;
		}
		public void setCost(int cost) {
			this.cost = cost;
		}

		public int getDiameter() {
			return diameter;
		}
		public void setDiameter(int diameter) {
			this.diameter = diameter;
		}
		
		public boolean update() {
			updatePath();
			x += dirX*velocity;
			y += dirY*velocity;
			
			return hitTestBase();
		}
		
		public void initPath(Graph g, BaseManager bases) {
			int[] ids_path = g.shortestPath(baseSrc.getIdBase(), baseDest.getIdBase());
			Base[] bases_path = new Base[ids_path.length];
			
			for(int i=0; i<ids_path.length; i++) {
				bases_path[i] = bases.getBase_manager().get(ids_path[i]);
			}
			path = bases_path;
			index_path = 1;
		}
		
		public void updatePath() {
			double norm = Math.sqrt( Math.pow(path[index_path].getX() - x, 2) + 
					                 Math.pow(path[index_path].getY() - y, 2) );
			
			dirX = (path[index_path].getX() - x)/norm;
			dirY = (path[index_path].getY() - y)/norm;
			
			if((Math.abs(path[index_path].getX() - x) <= path[index_path].getDiameter()/2) && (Math.abs(path[index_path].getY() - y) <= path[index_path].getDiameter()/2)){
				if(index_path < (path.length -1)) {
					index_path++;
				}
				else {
					x = baseDest.getX();
					y = baseDest.getY();
				}
			}
		}

		public boolean hitTestBase() {
	        if((Math.abs(baseDest.getX() - x) <= (baseDest.getDiameter()/2)) && (Math.abs(baseDest.getY() - y) <= (baseDest.getDiameter()/2))){
	        	baseDest.receiveGuardian(this);
	        	return true;
	        }
	        
	        return false;
		}
		
		public void receiveMissile(int effectLife, int effectVelocity) {
			this.life-=effectLife;
			this.velocity-=effectVelocity;
			if(this.velocity < 0) this.velocity = 0;
		}
		
		public void draw(Graphics g) {
			g.drawImage(imgAgent[idPlayer], (int)(x - diameter/2.), (int)(y - diameter/2.), diameter, diameter, null);
		}

	}
